package com.macro.mall.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsTourismProjectParam;
import com.macro.mall.mapper.BmsTourismProjectMapper;
import com.macro.mall.model.*;
import com.macro.mall.service.BmsTourismProjectService;
import com.macro.mall.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class BmsTourismProjectServiceImpl implements BmsTourismProjectService {

    @Autowired
    private BmsTourismProjectMapper bmsTourismProjectMapper;

    @Autowired
    private UserUtils userUtils;


    @Override
    public List<BmsTourismProject> getList(BmsTourismProjectParam bmsTourismProjectParam) {
        PageHelper.startPage(bmsTourismProjectParam.getPageNum(), bmsTourismProjectParam.getPageSize());
        BmsTourismProjectExample example = new BmsTourismProjectExample();
        BmsTourismProjectExample.Criteria criteria = example.createCriteria();
        criteria.andIsDelete(0);
        if (StringUtils.isNotBlank(bmsTourismProjectParam.getName())) {
            criteria.andNameLike("%" + bmsTourismProjectParam.getName() + "%");
        }
        if (Objects.nonNull(bmsTourismProjectParam.getIsExternal())) {
            criteria.andIsExternalEqualTo(bmsTourismProjectParam.getIsExternal());
        }
        example.setOrderByClause("create_time desc");
        List<BmsTourismProject> list = bmsTourismProjectMapper.selectByExample(example);
        return list;
    }

    /**
     * 获取文旅项目
     */
    @Override
    public List<BmsTourismProject> findAllList(){
        BmsTourismProjectExample example = new BmsTourismProjectExample();
        BmsTourismProjectExample.Criteria criteria = example.createCriteria();
        criteria.andIsDelete(0);
        return bmsTourismProjectMapper.selectByExample(example);
    }

    @Override
    public int create(BmsTourismProjectParam bmsTourismProjectParam) {
        BmsTourismProject bmsTourismProject = new BmsTourismProject();
        BeanUtils.copyProperties(bmsTourismProjectParam, bmsTourismProject);
        Long userId = userUtils.getUserId();
        bmsTourismProject.setCreateUser(userId);
        bmsTourismProject.setUpdateUser(userId);
        bmsTourismProject.setCreateTime(new DateTime());
        bmsTourismProject.setUpdateTime(new DateTime());
        int count = bmsTourismProjectMapper.insertSelective(bmsTourismProject);
        return count;
    }

    @Override
    public int update(BmsTourismProjectParam bmsTourismProjectParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsTourismProject bmsTourismProject = new BmsTourismProject();
        bmsTourismProject.setId(bmsTourismProjectParam.getId());
        BeanUtils.copyProperties(bmsTourismProjectParam, bmsTourismProject);
        bmsTourismProject.setUpdateTime(new DateTime());
        bmsTourismProject.setUpdateUser(userId);
        int conut = bmsTourismProjectMapper.updateByPrimaryKeySelective(bmsTourismProject);
        return conut;
    }

    @Override
    public BmsTourismProject getItem(Long id) {
        return bmsTourismProjectMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(List<Long> ids) {
        BmsTourismProjectExample example = new BmsTourismProjectExample();
        example.createCriteria().andIdIn(ids);
        Long userId = userUtils.getUserId();
        BmsTourismProject updateDetail = new BmsTourismProject();
        updateDetail.setUpdateUser(userId);
        updateDetail.setUpdateTime(new Date());
        updateDetail.setIsDelete((byte) 1);
        return bmsTourismProjectMapper.updateByExampleSelective(updateDetail,example);
    }
}
